package com.ipt.frm.hola;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ipt.frm.hola.bean.Student;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

@Path("/Goodby")
@Api(value = "/Goodby", description = "Jersey say hello")
public class Goodby {

	// This method is called if HTML is request
	@GET
	@Produces(MediaType.TEXT_HTML)
	@ApiOperation(value = "Arratzaldeon World", notes = "Anything Else?")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
			@ApiResponse(code = 500, message = "Something wrong in Server") })
	public String sayHtmlHello() {
		return "<html> " + "<title>" + "Hello Jersey</title>" + "<body><h1>"
				+ "Hello Jersey" + "</body></h1>" + "</html> ";
	}

	// This method is called if TEXT_PLAIN is request

	@GET
	// @Path("/{param1}{param2}")
	@Produces(MediaType.TEXT_PLAIN)
	@ApiOperation(value = "Say Hello World", notes = "Anything Else?")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
			@ApiResponse(code = 500, message = "Something wrong in Server") })
	public String sayPlainTextHello() {
		return "Hello Jersey";
	}

	// This method is called if XML is request

	@GET
	@Produces(MediaType.TEXT_XML)
	@ApiOperation(value = "Say Hello World", notes = "Anything Else?")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
			@ApiResponse(code = 500, message = "Something wrong in Server") })
	public String sayXMLHello() {
		return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey</hello>";
	}

	@GET
	@Path("/{param}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Student RetronoJson(@PathParam("param") String json) {
		Student student = null;
		return student;
	}

	/** Objeto conexion a la base de datos */
	// Connection conexion = null;
	/** Nombre del usuario a la BD */
	String dbUser = "root";
	/** Pass de la BD */
	String dbPass = "";
	/** Puerto de la base de datos */
	int dbPort = 3306;
	/** Nombre de la base de datos a conectarse */
	String dbName = "Usuarios";

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Student> sayJSONHello() {
		ArrayList<Student> lista = new ArrayList<Student>();
		Student st;

		st = new Student("Agamenon", "Diaz", 22, 1, "6666666", "pepe");
		lista.add(st);

		/*
		 * st = new Student("Raul", "Navascues", 31, 1, "6666666", "pepe");
		 * lista.add(st);
		 * 
		 * st = new Student("Heriberto", "Martin", 50, 1, "6666666", "pepe");
		 * lista.add(st);
		 * 
		 * st = new Student("Nestor", "Suverbiola", 30, 1, "6666666", "pepe");
		 * lista.add(st);
		 * 
		 * st = new Student("Iker", "Fernandez", 35, 1, "6666666", "pepe");
		 * lista.add(st);
		 */

		return lista;

	}

	// Aqui va la Base de datos

	/**
	 * Gestiona la conexion a la base de datos
	 * 
	 * @return Retorna la conexion.
	 */
	private Connection conectar() {
		Connection conexion = null;
		try {
			String dbUrl = "jdbc:mysql://localhost:" + dbPort + "/" + dbName;
			Class.forName("com.mysql.jdbc.Driver");
			conexion = DriverManager.getConnection(dbUrl, dbUser, dbPass);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conexion;
	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public void postMsg(Student estudiante) {

		Connection conexion = null;
		String sql = "";

		sql = "INSERT INTO usuario (nombre,apellido1,apellido2,telefono,edad) VALUES (?,?,?,?,?);";

		conexion = conectar();

		PreparedStatement stmt;
		try {
			stmt = conexion.prepareStatement(sql);

			stmt.setString(1, estudiante.getFirstName());
			stmt.setString(2, estudiante.getLastName());
			stmt.setString(3, estudiante.getApellido2());
			stmt.setString(4, estudiante.getPhone());
			stmt.setInt(5, estudiante.getAge());

			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
